﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Habi.Solver.Helpers
{
    public class NumberHelper
    {
        public static double Round(double val, int radix)
        {
            double newVal = Math.Ceiling(System.Convert.ToDouble(val) * System.Convert.ToDouble(Math.Pow(10, radix))) / System.Convert.ToDouble(Math.Pow(10, radix));
            string format = string.Format("{0:f" + radix + "}", newVal);
            return System.Convert.ToDouble(format);
        }
        public static decimal Round(decimal val, int radix)
        {
            decimal newVal = Math.Ceiling(System.Convert.ToDecimal(val) * System.Convert.ToDecimal(Math.Pow(10, radix))) / System.Convert.ToDecimal(Math.Pow(10, radix));
            string format = string.Format("{0:f" + radix + "}", newVal);
            return System.Convert.ToDecimal(format);
        }

        public static string Byt2Unit(long size)
        {
            double _size = Convert.ToDouble(size);
            string[] units = new string[] { "B", "KB", "MB", "GB", "TB", "PB" };
            double mod = 1024.0;
            int i = 0;
            while (_size >= mod)
            {
                _size /= mod; i++;
            }
            return Math.Round(_size, 2) + units[i];
        }
    }
}
